package com.hongkang.springtrains.web;

import com.alibaba.fastjson.JSONObject;
import com.hongkang.springtrains.model.User;
import com.hongkang.springtrains.web.config.WebConfig;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertThat;


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { WebConfig.class})
public class RestControllerTestWithMockMvc {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        System.out.println("before");
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    /**
     * No URL vars but contains request body vars
     */
    @Test
    public void getUserTestC() throws Exception {
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/users/userB")
                                                                             .header("testHeader","headerValue")
                                                                             .content("'name':'Do','email':'dujiang@sohu.com'");
        mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk())
                                       .andExpect(MockMvcResultMatchers.content().string("This is body"))
                                       .andDo(MockMvcResultHandlers.print());

    }


    @Test
    public  void testUserC() throws Exception{
        User user = new User();
        user.setName("杜江");
        user.setEmail("dujiang@do.com");
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/users/userC")
                .header("A","valueA")
                .header("B","valueB")
                //.contentType(MediaType.APPLICATION_JSON_UTF8)
                //.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
                //.characterEncoding("UTF_8")
                .content(JSONObject.toJSONString(user))
                //.cookie(new Cookie("username","dujiang"))
                ;
        mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andDo(MockMvcResultHandlers.print());

    }


    @Test
    public void testD(){
        assertThat(20, greaterThanOrEqualTo(20));

    }

    @AfterClass
    public static  void destory(){
        System.out.println("Destory");
    }

}
